package com.zhanglubao.lol.downloader;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.zhanglubao.lol.downloader.util.Logger;

import java.util.Map;

/**
 * Created by rocks on 15-6-28.
 */
public class DownloadService extends Service {

    private static final String TAG = "Download_Service";

    public static final String ACTION_CREATE = "create";
    public static final String ACTION_STAER_NEWTASK = "startNewTask";

    public static final String KEY_TASKID = "taskId";

    private DownloadServiceManager manager;

    private static long time = 0l;

    private IDownloadService.Stub mBinder = new IDownloadService.Stub() {
        @Override
        public void createDownload(String videoId, String videoName,String imgUrl)
                throws RemoteException {
            manager.createDownload(videoId, videoName,imgUrl);
        }

        @Override
        public void createDownloads(String[] videoIds, String[] videoNames,String[] imgUrls)
                throws RemoteException {
            manager.createDownloads(videoIds, videoNames,imgUrls);
        }

        @Override
        public void down(String taskId) throws RemoteException {
            manager.startDownload(taskId);
        }

        @Override
        public void pause(String taskId) throws RemoteException {
            manager.pauseDownload(taskId);
        }

        @Override
        public boolean delete(String taskId) throws RemoteException {
            return manager.deleteDownloading(taskId);
        }

        @Override
        public boolean deleteAll() throws RemoteException {
            return manager.deleteAllDownloading();
        }

        @Override
        public Map<String, DownloadInfo> getDownloadingData()
                throws RemoteException {
            return manager.getDownloadingData();
        }

        @Override
        public void registerCallback(ICallback cb) throws RemoteException {
            manager.registerCallback(cb);
        }

        @Override
        public void unregister() throws RemoteException {
            manager.unregister();
        }

        @Override
        public void refresh() throws RemoteException {
            manager.refresh();
        }

        @Override
        public void startNewTask() throws RemoteException {
            manager.startNewTask();
        }

        @Override
        public void stopAllTask() throws RemoteException {
            manager.stopAllTask();
        }

        @Override
        public String getCurrentDownloadSDCardPath() throws RemoteException {
            return manager.getCurrentDownloadSDCardPath();
        }

        @Override
        public void setCurrentDownloadSDCardPath(String path)
                throws RemoteException {
            manager.setCurrentDownloadSDCardPath(path);
        }

        @Override
        public boolean canUse3GDownload() throws RemoteException {
            return manager.canUse3GDownload();
        }

        @Override
        public void setCanUse3GDownload(boolean flag) throws RemoteException {
            manager.setCanUse3GDownload(flag);
        }



        @Override
        public int getDownloadFormat() throws RemoteException {
            return manager.getDownloadFormat();
        }

        @Override
        public void setDownloadFormat(int format) throws RemoteException {
            manager.setDownloadFormat(format);
        }

        @Override
        public void setTimeStamp(long time) throws RemoteException {
            manager.setTimeStamp(time);
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        Logger.d(TAG, "onBind()");
        return mBinder;
    }

    @Override
    public void onCreate() {
        Logger.d(TAG, "onCreate()");
        manager = DownloadServiceManager.getInstance();
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent == null) {
            return START_NOT_STICKY;
        }

        Logger.d(TAG, "onStartCommand():" + flags + "/" + startId + "/"
                + intent.getAction());
        if (ACTION_CREATE.equals(intent.getAction())) {
            long now = System.currentTimeMillis();
            if ((now - time) > 1000l) {
                time = now;
                String videoId = intent.getStringExtra("videoId");
                String videoName = intent.getStringExtra("videoName");
                String imgUrl = intent.getStringExtra("imgUrl");
                manager.createDownload(videoId, videoName,imgUrl);
            }
            return START_NOT_STICKY;
        } else if (ACTION_STAER_NEWTASK.equals(intent.getAction())) {
            manager.startNewTask();
        }
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        Logger.d(TAG, "onDestroy()");
        manager.destroy();
        super.onDestroy();
    }
}